Definition:
An algorithm is a step-by-step procedure or set of rules designed to solve a specific problem or perform a specific task. Think of it as a recipe for solving a problem.
An algorithm is a step-by-step procedure or set of rules designed to solve a specific problem or perform a specific task. Think of it as a recipe for solving a problem.
Real-life Example - Making Tea:
Step 1: Boil water
Step 2: Add tea leaves
Step 3: Add sugar
Step 4: Add milk
Step 5: Strain and serve
This is an algorithm for making tea! Similarly, algorithms in programming solve computational problems.
Step 1: Boil water
Step 2: Add tea leaves
Step 3: Add sugar
Step 4: Add milk
Step 5: Strain and serve
This is an algorithm for making tea! Similarly, algorithms in programming solve computational problems.
Characteristics of a Good Algorithm:
1. Input: Algorithm should have zero or more inputs (data we provide)
2. Output: Must produce at least one output (result)
3. Definiteness: Each step must be clear and unambiguous (no confusion)
4. Finiteness: Must terminate after finite number of steps (should not run forever)
5. Effectiveness: Each step must be simple enough to be executed
6. Correctness: Should produce correct output for all valid inputs
2. Output: Must produce at least one output (result)
3. Definiteness: Each step must be clear and unambiguous (no confusion)
4. Finiteness: Must terminate after finite number of steps (should not run forever)
5. Effectiveness: Each step must be simple enough to be executed
6. Correctness: Should produce correct output for all valid inputs
Example Algorithm - Find Maximum of Two Numbers:
Algorithm: FindMaximum
Input: Two numbers a and b
Output: Maximum number
Step 1: Start
Step 2: Read values of a and b
Step 3: If a > b then
max = a
Else
max = b
Step 4: Print max
Step 5: Stop
Algorithm vs Program:
β’ Algorithm: Language-independent solution (written in plain English or pseudocode)
β’ Program: Implementation of algorithm in a specific programming language (Java, C++, Python)
β’ Algorithm: Language-independent solution (written in plain English or pseudocode)
β’ Program: Implementation of algorithm in a specific programming language (Java, C++, Python)